package Blog;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

/**
 * @Author：徐志荣
 * @Date：2023/5/23 20:23
 */
public class BlogCases  extends InitAndEnd{
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("url",
                "博客详情页",
                ""
                ));
    }

    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")


    //输入正确的账号,密码 登录成

    @Test
    void LoginSuccess(String username,String password,String blog_url) throws InterruptedException {
        //打开博客登录页面
        webDriver.get("");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys("admin");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url = " " ,否则,测试不通过
        Assertions.assertEquals("",cur_url);
        //列表页展示用户信息是admin
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_admin = webDriver.findElement("").getText();
        Assertions.assertEquals(username,cur_admin);
        //用户名=admin,测试通过,否则测试不通过
    }


    //博客列表页的博客数量不为0
    @Test
    void BlogList(){
        //打开博客列表页
        webDriver.get("");
        //获取页面上所有博客标题对应的元素
        int title_number = webDriver.findElements(By.cssSelector(".title")).size();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //如果元素数量不为0,测试通过
        Assertions.assertNotEquals(0,title_number);
    }

    //博客详情页校验 : url+标题+页面的title=博客详情页
    @ParameterizedTest
    @MethodSource("Generator")
    @Test
    void BlogDetail(String expected_url,String expected_title,String expected_blog_title){
        //找到第一篇博客对应的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("")).click();
        //获取当前页面url
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_url  = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取当前页面title
        String cur_title = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //获取博客标题
        String cur_blog_title  = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);


    }

    //写博客
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客,点击
        webDriver.findElement(By.cssSelector("")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //通过js找到输入框,输入标题
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);
            //点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("url",cur_url);


    }
    //校验已发布博客标题
    //校验已发布博客时间
    @Test
    void BlogInfoChecked(){
        webDriver.get("url");
        //获取第一篇博客标题
        String cur_title = webDriver.findElement(By.cssSelector("")).getText();
        //获取第一篇博客发布时间
        String cur_time = webDriver.findElement(By.xpath("")).getText();
        //校验博客标题 =? 自动化测试
        Assertions.assertEquals("自动化测试",cur_title);
        if (cur_time.contains("2023-05-23")){
            System.out.println("测试通过 ");
        }else{
            System.out.println("测试不通过");
        }
        if (cur_time.contains("2023-05-23")){
            System.out.println("测试通过 ");
        }else{
            System.out.println("测试不通过");
        }



    }

    //删除和刚才发布的博客
    @Test
    void DeleteBlog(){
        //打开博客列表页面
        webDriver.get("url");
        //点击查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        webDriver.findElement(By.cssSelector("")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //点击删除按钮
        webDriver.findElement(By.cssSelector("")).click();
        //博客列表页第一篇博客标题 不是 "自动化测试"
        String blog_title = webDriver.findElement(By.xpath("")).getText();
        //校验不相等
        Assertions.assertNotEquals("自动化测试",blog_title);

    }

    //注销
    @Order(7)
    @Test
    void Logout(){
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("")).click();
        //校验url(登录url)
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(cur_url);
        //校验提交按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull();


    }


}
